package hrms.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import hrms.entity.Emp;
import hrms.service.EmpServiceImpl;
import hrms.util.LogUtil;
import hrms.util.Result;

@Controller
@RequestMapping("/emp")
public class EmpController {
	
	@Resource(name="empService")
	private EmpServiceImpl empService;
	
	@ExceptionHandler
	public String exHandle(Exception ex, HttpServletRequest req) {
		LogUtil.saveEx(ex);
		return "error";
	}
	
	@ResponseBody
	@RequestMapping("/loadEmp.sls")
	public Result<List<Emp>> executeA(HttpServletRequest req ,HttpServletResponse res){
		String page = req.getParameter("page");
		String deptId = req.getParameter("deptid");
		if(deptId == null){
			deptId = "";
		}
		Result<List<Emp>> result = empService.findByPage(page,res,deptId);
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/empPage.sls")
	public Result<Map<String,Integer>> executeB2(HttpServletRequest req){
		Result<Map<String,Integer>> result = empService.pageTotal(req);
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/addEmp.sls")
	public Result<Object> executeB3(HttpServletRequest req){
		String empName = req.getParameter("empName");
		String empAge = req.getParameter("empAge");
		String empSex = req.getParameter("empSex");
		String empJob = req.getParameter("empJob");
		String empDept = req.getParameter("empDept");
		return empService.addEmp( empName,  empSex, empAge, empJob, empDept);
	}
	
	@ResponseBody
	@RequestMapping("/updateEmp.sls")
	public Result<Object> executeB4(HttpServletRequest req){
		String empId = req.getParameter("empId");
		String empName = req.getParameter("empName");
		String empAge = req.getParameter("empAge");
		String empSex = req.getParameter("empSex");
		String empJob = req.getParameter("empJob");
		String empDept = req.getParameter("empDept");
		return empService.uppdateEmp(empId, empName,  empSex, empAge, empJob, empDept,req);
	}
	
	@ResponseBody
	@RequestMapping("/deleteEmp.sls")
	public Result<Object> executeI(HttpServletRequest req){
		String empId = req.getParameter("id");
		return empService.deleteEmp(empId, req);
	}
	
	@ResponseBody
	@RequestMapping("/showEmp.sls")
	public Result<Emp> executeII(HttpServletRequest req){
		String empId = req.getParameter("empId");
		return empService.findEmp(empId, req);
	}
	
	
	@ResponseBody
	@RequestMapping("/searchEmp.sls")
	public Result<List<Emp>> executeAA(HttpServletRequest req ,HttpServletResponse res){
		String name = req.getParameter("name");
		if(name == null || name == ""){
			return empService.findByPage("1",res,"");
		}
		return empService.search(name);
	}
}